﻿using System;
using System.Windows.Forms;
using Bonesoft.Controls;
using Business;
using Business.Entity.Contabil;
using Business.Entity.Contabil.Pessoa;
using Business.Entity.Contabil.Pessoa.SubClass.Usuario.ClassesRelacionadas;
using Business.Forms;
using Business.Seguranca;
using Business.Validation;
using DevExpress.XtraEditors;

namespace Bonesoft.Forms.Contabil
{
    public partial class FormCustosFixos : FormDefault, IForm
    {
        public FormCustosFixos()
        {
            InitializeComponent();
            // Comandos de manipulação de dados e formulário.
            cmdSalvar.Form = this;
            cmdExcluir.Form = this;
            cmdCancelar.Form = this;


            pessoaBindingSource.DataSource = PessoaRepository.GetList();
            tipoTituloBindingSource.DataSource = TipoLancamentoRepository.GetList();
        }

        private void repositoryControl1_AntesDeSalvar(object source, RepositoryControlEventArgs e)
        {
            custoFixoBindingSource.EndEdit();
        }

        public object Novo()
        {
            var c = new CustoFixo();
            CurrentObject = c;
            pessoaSearchLookUpEdit.Focus();
            ShowDialog();
            if (c.Id == 0)
            {
                return null;
            }
            return c;
        }

        public void Alterar(object entity)
        {
            CurrentObject = entity;
            custoFixoBindingSource.DataSource = entity;
            pessoaSearchLookUpEdit.Focus();
            ShowDialog();
        }

        public object Save()
        {
            if (Validate().HasError)
            {
                CustoFixoRepository.Save((CustoFixo)CurrentObject);
                return CurrentObject;
            }
            return CurrentObject;
        }

        public void Update(object entity)
        {
            Alterar(entity);
        }

        public bool Delete(object entity)
        {
            CurrentObject = entity;
            CustoFixoRepository.Delete((CustoFixo) entity);
            return true;
        }

        public object CurrentObject
        {
            get
            {
                custoFixoBindingSource.EndEdit();
                return custoFixoBindingSource.DataSource;
            }
            set
            {
                custoFixoBindingSource.DataSource = value;
            }
        }

        public PermissaoForm Permissao
        {
            get
            {
                return SegurancaUtils.GetPermissaoForm(Name);
            }
        }

        public EntityValidationResult Validate()
        {
            return DataValidation.ValidateEntity((CustoFixo) CurrentObject);
        }

        public void Reload()
        {
            
        }

        public void Cancel()
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        public Object New()
        {
            return Novo();
        }

        public XtraForm Form
        {
            get
            {
                return this;
            }
        }

        public FormState FormState { get; set; }
    }
}